package ar.edu.unq.wannaplay.pages.abm.bands;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.unq.wannaplay.dtos.CareerPlanOrganizerDTO;
import ar.edu.unq.wannaplay.ocupation.Percentages;
import ar.edu.unq.wannaplay.pages.BandPage;
import ar.edu.unq.wannaplay.pages.abm.WPABM;
import ar.edu.unq.wannaplay.services.CareerPlanService;
import ar.edu.unq.wannaplay.tools.PercentagesSelector;

public class AddBandPage extends WPABM<String, BandPage> {
    private static final long serialVersionUID = 1L;

    @SpringBean(name = "service.careerPlanService")
    private CareerPlanService careerPlanService;

    private CareerPlanOrganizerDTO careerPlan;

    private List<Integer> percentages;

    private PercentagesSelector percentageCreator;

    public AddBandPage() {
    	super(BandPage.class, "");
        this.init();
    }

    @Override
    protected void accept() {
        this.getCareerPlan().setModifyedRank(this.getAbmObject());
        Percentages percentage = new Percentages();
        percentage.setPercentagesList(this.getPercentages());
        this.getCareerPlan().setNewRanges(percentage);
        this.getCareerPlanService().updateModifyRanges(this.getCareerPlan());
    }

    protected void init() {
    	careerPlan = this.getCareerPlanService().getCareerPlanOrganizer();
    	percentages = new ArrayList<Integer>();
    	this.setPercentageCreator(new PercentagesSelector());
    	
        WebMarkupContainer container = new WebMarkupContainer("container");
        container.add(new TextField<String>("bandName", new PropertyModel<String>(this.getCareerPlan(), "name")));

        container.add(new PageableListView<Integer>("percentageTable", this.getPercentages(), 5) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<Integer> item) {
                Integer percentage = item.getModelObject();
                item.add(new Label("percentage", percentage.toString()));
                item.add(new Link<WebPage>("removePercentage") {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public void onClick() {
                        AddBandPage.this.getPercentages().remove(item);
                        AddBandPage.this.init();
//                        this.setResponsePage(AddBandPage.class);
                    }
                });
            }
        });

        container.add(new TextField<PercentagesSelector>("newPercentage", new PropertyModel<PercentagesSelector>(this
                .getPercentageCreator(), "percentage")));

        container.add(new Link<WebPage>("addPercentage") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                AddBandPage.this.getPercentages().add(AddBandPage.this.getPercentageCreator().getPercentage());
                AddBandPage.this.init();
//              this.setResponsePage(AddBandPage.class);
            }

        });
    }

    // setters&getters

    public void setCareerPlanService(final CareerPlanService careerPlanService) {
        this.careerPlanService = careerPlanService;
    }

    public CareerPlanService getCareerPlanService() {
        return careerPlanService;
    }

    public void setCareerPlan(final CareerPlanOrganizerDTO careerPlan) {
        this.careerPlan = careerPlan;
    }

    public CareerPlanOrganizerDTO getCareerPlan() {
        return careerPlan;
    }

    public void setPercentages(final List<Integer> percentages) {
        this.percentages = percentages;
    }

    public List<Integer> getPercentages() {
        return percentages;
    }

    public void setPercentageCreator(final PercentagesSelector percentageCreator) {
        this.percentageCreator = percentageCreator;
    }

    public PercentagesSelector getPercentageCreator() {
        return percentageCreator;
    }
}
